<template>
    <div>
        <template v-if="itemdata.lMustInput === '0'">
            <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;">{{ itemdata.sTitle }}</div>
            <input type="hidden" v-model="itemdata.valuehidden" />
            <input type="hidden" v-model="itemdata.value" />
          <van-uploader v-if="itemdata.sFormat== 'refer:only-camera'"  placeholder="请上传图片" v-model="itemdata.valuelist"  capture="camera" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField" >
          </van-uploader>
          <van-uploader v-else  placeholder="请上传图片" v-model="itemdata.valuelist"   :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField" >
          </van-uploader>
        </template>
        <template v-if="itemdata.lMustInput === '1'">
            <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;"><span style="color:red">*</span>{{ itemdata.sTitle }}</div>
            <input type="hidden" v-model="itemdata.valuehidden" />
            <input type="hidden" v-model="itemdata.value" />
          <van-uploader v-if="itemdata.sFormat== 'refer:only-camera'"  placeholder="请上传图片" v-model="itemdata.valuelist"  capture="camera" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField" >
          </van-uploader>
          <van-uploader v-else  placeholder="请上传图片" v-model="itemdata.valuelist"   :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField" >
          </van-uploader>
        </template>
    </div>
</template>
<script>
import { Toast } from 'vant'
import Vue from 'vue'
Vue.use(Toast)
export default {
  props: {
    jsondata: {
      type: Object,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  methods: {
    onImageHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('image' + i, deviceFile[i].file, deviceFile[i].name)
      }
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '等待上传中...请稍候'
      })
      this.axios({
        method: 'post',
        url: '/api/upload.imagedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        // 清掉提示
        toast.clear()
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname + ';' + headData[i].valuehidden
                headData[i].value = fileInfo[0].oldname + ';' + headData[i].value
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delImageHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let imgVal = headData[i].valuehidden.split(';')
            let imgTitle = headData[i].value.split(';')
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] === file.file.name) {
                imgVal[j] = ''
                imgTitle[j] = ''
              }
            }
            let imgVal1 = ''
            let imgTitle1 = ''
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] !== '') {
                imgVal1 += imgVal[j] + ';'
                imgTitle1 += imgTitle[j] + ';'
              }
            }
            headData[i].valuehidden = imgVal1
            headData[i].value = imgTitle1
          }
        }
      }
      Toast('删除成功')
    }
  }
}
</script>
